package model;

import java.io.Serializable;

import javax.persistence.*;

import dtos.ExcursionDTO;
import dtos.PredefinedTPDTO;
import authentication.ejb.usermanagement.User;

import java.util.LinkedList;
import java.util.List;


/**
 * The persistent class for the predefinedTP database table.
 * 
 */
@Entity
@NamedQuery(name="PredefinedTP.findAll", query="SELECT p FROM PredefinedTP p")
public class PredefinedTP implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int travelID;


	//bi-directional many-to-many association to Excursion
	@ManyToMany(cascade={CascadeType.PERSIST})
	@JoinTable(
		name="excursionPredTP"
		, joinColumns={
			@JoinColumn(name="travelID")
			}
		, inverseJoinColumns={
			@JoinColumn(name="excursionID")
			}
		)
	private List<Excursion> excursions;
	
	//bi-directional many-to-one association to ExcursionPredTP
	@OneToMany(mappedBy="predefinedtp",cascade={CascadeType.PERSIST})
	private List<ExcursionPredTP> excursionPredTps;

	//bi-directional many-to-one association to Hotel
	@ManyToOne
	@JoinColumn(name="hotel")
	private Hotel hotelBean;

	//bi-directional many-to-one association to MeanOfTransportation
	@ManyToOne
	@JoinColumn(name="departureTransportation")
	private MeanOfTransportation meanOfTransportation1;

	//bi-directional many-to-one association to MeanOfTransportation
	@ManyToOne
	@JoinColumn(name="returnTransportation")
	private MeanOfTransportation meanOfTransportation2;

	//bi-directional many-to-one association to User
	@ManyToOne
	@JoinColumn(name="employeeID")
	private User user;

	public PredefinedTP() {
	}
	
	public PredefinedTP(PredefinedTPDTO predefinedTPDTO){
		super();
		//this.excursions = predefinedTPDTO.getExcursions();
		this.travelID = predefinedTPDTO.getTravelID();
		this.user = new User(predefinedTPDTO.getUser());
	}
	
	public PredefinedTPDTO getDTO(){
		PredefinedTPDTO p = new PredefinedTPDTO();
		p.setExcursionsDTO(this.getExcursionsDTO());
		p.setHotelBean(this.hotelBean.getDTO());
		p.setMeanOfTransportation1(this.meanOfTransportation1.getDTO());
		p.setMeanOfTransportation2(this.meanOfTransportation2.getDTO());
		p.setTravelID(this.travelID);
		p.setUser(this.user.getUserDTO());
		return p;
	}

	public int getTravelID() {
		return this.travelID;
	}

	public void setTravelID(int travelID) {
		this.travelID = travelID;
	}

	public List<Excursion> getExcursions() {
		return this.excursions;
	}
	
	public List<ExcursionDTO> getExcursionsDTO() {
		List<ExcursionDTO> res = new LinkedList<ExcursionDTO>();
		for (Excursion e : this.excursions){
			res.add(e.getDTO());
		}
		return res;
		
	}

	public void setExcursions(List<Excursion> excursions) {
		this.excursions = excursions;
	}
	
	public void addExcursion(Excursion e){
		this.excursions.add(e);
	}
	
	public void removeExcursion(Excursion e){
		this.excursions.remove(e);
	}

	public Hotel getHotelBean() {
		return this.hotelBean;
	}

	public void setHotelBean(Hotel hotelBean) {
		this.hotelBean = hotelBean;
	}

	public MeanOfTransportation getMeanOfTransportation1() {
		return this.meanOfTransportation1;
	}

	public void setMeanOfTransportation1(MeanOfTransportation meanOfTransportation1) {
		this.meanOfTransportation1 = meanOfTransportation1;
	}

	public MeanOfTransportation getMeanOfTransportation2() {
		return this.meanOfTransportation2;
	}

	public void setMeanOfTransportation2(MeanOfTransportation meanOfTransportation2) {
		this.meanOfTransportation2 = meanOfTransportation2;
	}
	

	public List<ExcursionPredTP> getExcursionPredTps() {
		return this.excursionPredTps;
	}

	public void setExcursionPersTps(List<ExcursionPredTP> excursionPredTps) {
		this.excursionPredTps = excursionPredTps;
	}

	public User getUser() {
		return this.user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public boolean isFromTo(String from, String to) {
		return ((this.meanOfTransportation1.getTrip().getDeparturePlace().toLowerCase().equals(from.toLowerCase()))&&(this.meanOfTransportation1.getTrip().getArrivalPlace().toLowerCase().equals(to.toLowerCase())));
	}

}